//#define __DEBUG__
// by Tyrghen
#define THIS_FILE "fn_createfootmen.sqf"
#include "x_setup.sqf"
private ["_type","_startpoint","_attackpoint","_heliendpoint","_number_groups","_stop_it","_current_target_pos","_dummy","_new_current_target_pos","_cur_radius","_paragrp","_unit_array","_real_units","_one_unit"
		];
if !(call FUNC(checkSHC)) exitWith {};

PARAMS_4(_startpoint,_attackpoint,_heliendpoint,_number_groups);
__TRACE_1("","_this")

__INC(_number_groups);
GVAR(should_be_there) = _number_groups;

GVAR(c_attacking_grps) = [];

_dummy = GVAR(target_names) select __XJIPGetVar(GVAR(current_target_index));
_current_target_pos = _dummy select 0;
_stop_it = false;

#ifndef __TT__
if ((__XJIPGetVar(GVAR(searchintel)) select 1) == 1) then {
	[QGVAR(kbmsg), [43]] call FUNC(NetCallEventCTS);
};
#endif



for "_i" from 1 to _number_groups do {
	if (__XJIPGetVar(GVAR(mt_radio_down))) exitWith {_stop_it = true};
	_dummy = GVAR(target_names) select __XJIPGetVar(GVAR(current_target_index));
	_new_current_target_pos = _dummy select 0;
	if (_new_current_target_pos distance _current_target_pos > 500) exitWith {_stop_it = true};

	_cur_radius = _dummy select 2;
	__GetEGrp(_paragrp)
	_unit_array = ["heli", GVAR(enemy_side)] call FUNC(getunitliste);
	_real_units = _unit_array select 0;
	_unit_array = nil;
	sleep 0.1;
	{
		_one_unit = _paragrp createunit [_x, [10,10,0], [], 300,"NONE"];
		_one_unit call FUNC(removeNVGoggles);
		_one_unit call FUNC(removefak);
		_one_unit call FUNC(removetech);
		_one_unit call FUNC(addInsignia);
		_one_unit setVariable ["BIS_noCoreConversations", true];
		if (GVAR(domdatabase)) then {
			[_one_unit, {_this addEventHandler ["killed", {if (isPlayer (_this select 1)) then {[QGVAR(PAIKP), _this select 1] call FUNC(NetCallEventCTS)}}]}] call FUNC(setUnitCode);
		};
#ifdef __TT__
		[_one_unit, {_this addMPEventHandler ["MPkilled", {if (isServer) then {[[15, 3, 2, 1],_this select 1, _this select 0] call FUNC(AddKills);(_this select 0) removeAllMPEventHandlers "MPKilled"}}]}] call FUNC(setUnitCode);
#endif
		if (GVAR(with_ai) && {GVAR(with_ranked)}) then {
			[_one_unit, {_this addEventHandler ["killed", {if (!isPlayer (_this select 1)) then {[QGVAR(AddKillAI), [1,_this select 1]] call FUNC(NetCallEventCTS)}}]}] call FUNC(setUnitCode);
		};
		_one_unit setSkill ((GVAR(skill_array) select 0) + (random (GVAR(skill_array) select 1)));
		// TYRGHEN - Adds some built in scripted event handlers to units to make them more reactive
		if (!(isNil QGVAR(with_aihandlers)) && {GVAR(with_aihandlers) == 0}) then {
			_one_unit call FUNC(scriptedreactions);
		};
		
		_one_unit setPos (GVAR(enemyai_mt_camp_pos));
		sleep 0.551;
	} forEach _real_units;
	_paragrp allowFleeing 0;
	_paragrp setCombatMode "YELLOW";
	_paragrp setBehaviour "CARELESS";
	
	[_paragrp, _current_target_pos, _cur_radius] spawn {
		scriptName "spawn_x_createfootmen_usegroup";
		private ["_grp", "_pos", "_rad"];
		PARAMS_3(_grp,_pos,_rad);
		sleep 30;
		if ((_grp call FUNC(GetAliveUnitsGrp)) > 0) then {
			[_grp, _pos, [_pos, _rad], [10, 20, 50], "this call d_fnc_grmakesearch"] spawn FUNC(MakePatrolWPX);
			_grp setVariable [QGVAR(PATR),true];
			if (GVAR(doRespawnGroups)) then {
				GVAR(reduce_groups) set [count GVAR(reduce_groups), _grp];
			};
		};
	};
	
	GVAR(c_attacking_grps) set [count GVAR(c_attacking_grps), _paragrp];
	
	sleep 0.112;
	__DEC(GVAR(should_be_there));
	
	if (__XJIPGetVar(GVAR(mt_radio_down))) exitWith {_stop_it = true};
	sleep 40 + random 30;
};
if (_stop_it) then {
	__TRACE_1("Stopped","_stop_id")
};

if (!__XJIPGetVar(GVAR(mt_radio_down))) then {
	sleep 20.0123;
	if (count GVAR(c_attacking_grps) > 0) then {
		[GVAR(c_attacking_grps)] spawn FUNC(handleattackgroups);
	} else {
		GVAR(c_attacking_grps) = [];
		GVAR(create_new_paras) = true;
	};
};